package com.guoguo.controller;

import java.text.MessageFormat;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.guoguo.base.BaseController;
import com.guoguo.base.PageBean;
import com.guoguo.entity.TaskInfo;
import com.guoguo.entity.Users;
import com.guoguo.enums.TaskEnum;
import com.guoguo.service.TaskInfoService;
import com.guoguo.utils.StringUtils;

/**
 * Created by Administrator on 2017/11/8 0008.
 */

     /*
                           _ooOoo_
                          o8888888o
                          88" . "88
                          (| -_- |)
                          O\  =  /O
                       ____/`---'\____
                     .'  \\|     |//  `.
                    /  \\|||  :  |||//  \
                   /  _||||| -:- |||||-  \
                   |   | \\\  -  /// |   |
                   | \_|  ''\---/''  |   |
                   \  .-\__  `-`  ___/-. /
                 ___`. .'  /--.--\  `. . __
              ."" '<  `.___\_<|>_/___.'  >'"".
             | | :  `- \`.;`\ _ /`;.`/ - ` : | |
             \  \ `-.   \_ __\ /__ _/   .-` /  /
        ======`-.____`-.___\_____/___.-`____.-'======
                           `=---='
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                 佛祖保佑       永无BUG
        */
@Controller
@RequestMapping("/view/task")
public class TaskInfoController extends BaseController {

    @Autowired
    private TaskInfoService taskInfoService;

    /**
     * 列表页
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/taskList", method = RequestMethod.GET)
    public ModelAndView taskList(@RequestParam(required = false) Integer pageNumber, @RequestParam(required = false) Integer pageSize) {
        Map<String, Object> data = new HashedMap();
        Map<String, Object> param = new HashedMap();
        param.put("pageNumber", pageNumber);
        param.put("pageSize", pageSize);

        // TODO: 2017/11/13 0013 表格数据
        PageBean<TaskInfo> role = taskInfoService.getTaskInfoList(param);
        data = initData(data);

        data.put("taskInfoList", role);
        return returnView("/task/taskInfoList", data);
    }

    /**
     * 列表页
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
//    @ResponseBody
    @RequestMapping(value = "/searchTaskList", method = RequestMethod.GET)
    public ModelAndView searchTaskList(@RequestParam(required = false) Integer pageNumber, @RequestParam(required = false) Integer pageSize,
                                       @RequestParam(required = false) Integer giftType, @RequestParam(required = false) Integer status,
                                       @RequestParam(required = false) Integer resultSort, @RequestParam(required = false) Integer orderType) {
        Map<String, Object> data = new HashedMap();
        Map<String, Object> param = new HashedMap();
        param.put("pageNumber", pageNumber);
        param.put("pageSize", pageSize);
        param.put("giftType", giftType);
        param.put("status", status);
        param.put("resultSort", resultSort);
        param.put("orderType", orderType);

        // TODO: 2017/11/13 0013 表格数据
        PageBean<TaskInfo> role = taskInfoService.getTaskInfoList(param);
        data = initData(data);

        data.put("taskInfoList", role);
        data.put("giftType", giftType);
        data.put("giftStatus", status);
        data.put("resultSort", resultSort);
        data.put("orderType", orderType);
        return returnView("/task/taskInfoList", data);
    }

    /**
     * 跳转到新增或者修改页面
     *
     * @return
     */
    @RequestMapping(value = "/toTaskInfo", method = RequestMethod.GET)
    public ModelAndView saveOrUpdateTaskInfo(@RequestParam(required = false) String taskId) {
        Map<String, Object> data = new HashedMap();
        TaskInfo taskInfo = new TaskInfo();
        TaskInfo taskInfo1 = null;
        if (!StringUtils.isEmptyString(taskId)) {
            taskInfo.setId(taskId);
            taskInfo1 = taskInfoService.findTaskInfo(taskInfo);

        }
        data.put("taskInfo", taskInfo1 == null ? new TaskInfo() : taskInfo1);

        data = initData(data);
        return returnView("/task/taskInfo", data);
    }

    @ResponseBody
    @RequestMapping(value = "/saveOrUpdateTaskInfo", method = RequestMethod.POST)
    public JSONObject saveOrUpdateTaskInfo(HttpServletRequest request, TaskInfo taskInfo) {
        Users user = (Users) request.getSession().getAttribute("user");
        if (user != null) {
            taskInfo.setCreateUserId(user.getUserId());
        }
        taskInfoService.saveOrUpdateTaskInfo(taskInfo);
        return returnResult(TaskEnum.TASK_REQUEST_SUCCESS.getId(), null, TaskEnum.TASK_REQUEST_SUCCESS.getValue());
    }

    /**
     * 批量修改状态
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateBatch", method = RequestMethod.POST)
    public JSONObject updateBatch(@RequestParam(required = false) String ids, @RequestParam(required = false) int status) {
        int num = taskInfoService.updateBatch(ids, status);
        if (num > 0) {
            return returnResult(TaskEnum.TASK_BATCH_SUCCESS.getId(), null, MessageFormat.format(TaskEnum.TASK_BATCH_SUCCESS.getValue(), num));
        } else {
            return returnResult(TaskEnum.TASK_BATCH_SUCCESS.getId(), null, MessageFormat.format(TaskEnum.TASK_BATCH_SUCCESS.getValue(), num));
        }
    }

    /**
     * 删除任务
     *
     * @param taskId
     * @return
     */
    @RequestMapping(value = "/removeTaskInfo", method = RequestMethod.GET)
    public String removeTaskInfo(@RequestParam String taskId) {
        TaskInfo taskInfo = new TaskInfo();
        taskInfo.setId(taskId);
        int i = taskInfoService.removeTaskInfo(taskInfo);
        return "redirect:/view/task/taskList?pageNumber=1&pageSize=10";
    }


    /**
     * 下拉列表数据
     *
     * @param data
     * @return
     */
    private Map<String, Object> initData(Map<String, Object> data) {
        // TODO: 2017/11/14 0014 礼包领取条件
        Map<Integer, String> requireTypeMap = new TreeMap<>();
        requireTypeMap.put(TaskEnum.TASK_REQUIRE_INVITE.getId(), TaskEnum.TASK_REQUIRE_INVITE.getValue());
        requireTypeMap.put(TaskEnum.TASK_REQUIRE_LIKE.getId(), TaskEnum.TASK_REQUIRE_LIKE.getValue());
        requireTypeMap.put(TaskEnum.TASK_REQUIRE_COMMENT.getId(), TaskEnum.TASK_REQUIRE_COMMENT.getValue());
        requireTypeMap.put(TaskEnum.TASK_REQUIRE_SHARE.getId(), TaskEnum.TASK_REQUIRE_SHARE.getValue());
        requireTypeMap.put(TaskEnum.TASK_REQUIRE_LOGIN.getId(), TaskEnum.TASK_REQUIRE_LOGIN.getValue());
        requireTypeMap.put(TaskEnum.TASK_REQUIRE_LV.getId(), TaskEnum.TASK_REQUIRE_LV.getValue());
        requireTypeMap.put(TaskEnum.TASK_REQUIRE_SIGN.getId(), TaskEnum.TASK_REQUIRE_SIGN.getValue());
        // TODO: 2017/11/14 0014  重置周期
        Map<Integer, String> cycleMap = new TreeMap<>();
        cycleMap.put(TaskEnum.TASK_RESET_DAY.getId(), TaskEnum.TASK_RESET_DAY.getValue());
        cycleMap.put(TaskEnum.TASK_RESET_WEEK.getId(), TaskEnum.TASK_RESET_WEEK.getValue());
        cycleMap.put(TaskEnum.TASK_RESET_MONTH.getId(), TaskEnum.TASK_RESET_MONTH.getValue());
        // TODO: 2017/11/14 0014  礼包内容
        Map<Integer, String> prizeTypeMap = new TreeMap<>();
        prizeTypeMap.put(TaskEnum.TASK_REWARD_POINT.getId(), TaskEnum.TASK_REWARD_POINT.getValue());
        prizeTypeMap.put(TaskEnum.TASK_REWARD_EXP.getId(), TaskEnum.TASK_REWARD_EXP.getValue());
        prizeTypeMap.put(TaskEnum.TASK_REWARD_GIFT.getId(), TaskEnum.TASK_REWARD_GIFT.getValue());
        prizeTypeMap.put(TaskEnum.TASK_REWARD_PROPS.getId(), TaskEnum.TASK_REWARD_PROPS.getValue());
        // TODO: 2017/11/13 0013 礼包类型 查询页面专用
        Map<Integer, String> giftTypeMap = new TreeMap<>();
        giftTypeMap.put(-1, "==不限==");
        giftTypeMap.put(TaskEnum.TASK_GIFT_SIGN.getId(), TaskEnum.TASK_GIFT_SIGN.getValue());
        giftTypeMap.put(TaskEnum.TASK_GIFT_DAY.getId(), TaskEnum.TASK_GIFT_DAY.getValue());
        // TODO: 2017/11/15 0015 产品说：除了7个固定的周签到外，永远都不会再增加新的“签到”类型。增加修改页面专用
        Map<Integer, String> giftTypeMap1 = new TreeMap<>();
        giftTypeMap1.put(-1, "==不限==");
        giftTypeMap1.put(TaskEnum.TASK_GIFT_DAY.getId(), TaskEnum.TASK_GIFT_DAY.getValue());
        // TODO: 2017/11/13 0013 礼包状态
        Map<Integer, String> giftStatusMap = new TreeMap<>();
        giftStatusMap.put(-1, "==不限==");
        giftStatusMap.put(TaskEnum.TASK_STATUS_HIDDEN.getId(), TaskEnum.TASK_STATUS_HIDDEN.getValue());
        giftStatusMap.put(TaskEnum.TASK_STATUS_NORMAL.getId(), TaskEnum.TASK_STATUS_NORMAL.getValue());
        // TODO: 2017/11/13 0013 结果排序
        Map<Integer, String> resultSortMap = new TreeMap<>();
        resultSortMap.put(-1, "==默认排序==");
        resultSortMap.put(0, "序号");
        resultSortMap.put(1, "创建时间");
        // TODO: 2017/11/13 0013 排序方式
        Map<Integer, String> orderTypeMap = new TreeMap<>();
        orderTypeMap.put(0, "正序");
        orderTypeMap.put(1, "逆序");

        data.put("giftTypeMap", giftTypeMap);
        data.put("giftTypeMap1", giftTypeMap1);
        data.put("giftStatusMap", giftStatusMap);
        data.put("resultSortMap", resultSortMap);
        data.put("orderTypeMap", orderTypeMap);
        data.put("requireTypeMap", requireTypeMap);
        data.put("cycleMap", cycleMap);
        data.put("prizeTypeMap", prizeTypeMap);
        return data;
    }
}
